package com.tdrc.common.exception;

import com.tdrc.common.util.JsonResult;
import com.tdrc.common.beans.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author dpf
 * @version 1.0
 * @date 2020-4-9 17:31
 * @instruction 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    private static final String AUTH_SHIRO_EXPECTION="org.apache.shiro.authz.UnauthorizedException";
    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e)
    {
        log.info("===============================发现异常===================================");
        // 无权限异常
        if(e.toString().contains(AUTH_SHIRO_EXPECTION)){
            return new JsonResult(ResultCode.PERMISSION_UNAUTHORISE);
        }else{
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return new JsonResult(ResultCode.ERROR,"发生未知错误，请联系管理员！",e.getMessage());
        }
    }

}
